package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.Role;

/**
 * 角色表 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface RoleMapper {

	/**
	 * 根据条件分页查询角色数据
	 * 
	 * @param role 角色信息
	 * @return 角色数据集合信息
	 */
	@Select("<script>select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, "
			+ "r.status, r.del_flag, r.create_time, r.remark " + "from t_role r "
			+ "left join t_user_role ur on ur.role_id = r.role_id " + "left join t_user u on u.user_id = ur.user_id "
			+ "left join t_dept d on u.dept_id = d.dept_id " + "where r.del_flag = '0' "
			+ "<if test=\"roleId != null and roleId != 0\">AND r.role_id = #{roleId}</if> "
			+ "<if test=\"roleName != null and roleName != ''\">AND r.role_name like concat('%', #{roleName}, '%')</if> "
			+ "<if test=\"status != null and status != ''\">AND r.status = #{status}</if> "
			+ "<if test=\"roleKey != null and roleKey != ''\">AND r.role_key like concat('%', #{roleKey}, '%')</if> "
			+ "<if test=\"dataScope != null and dataScope != ''\">AND r.data_scope = #{dataScope}</if> "
			+ "<if test=\"params != null and params.beginTime != null and params.beginTime != ''\">and date_format(r.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')</if> "
			+ "<if test=\"params != null and params.endTime != null and params.endTime != ''\">and date_format(r.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')</if> "
			+ "${params.dataScope}</script>")
	public List<Role> selectRoleList(Role role);

	/**
	 * 根据用户ID查询角色
	 * 
	 * @param userId 用户ID
	 * @return 角色列表
	 */
	@Select("select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, "
			+ "r.status, r.del_flag, r.create_time, r.remark " + "from t_role r "
			+ "left join t_user_role ur on ur.role_id = r.role_id " + "left join t_user u on u.user_id = ur.user_id "
			+ "left join t_dept d on u.dept_id = d.dept_id " + "WHERE r.del_flag = '0' and ur.user_id = #{userId}")
	public List<Role> selectRolesByUserId(Long userId);

	/**
	 * 通过角色ID查询角色
	 * 
	 * @param roleId 角色ID
	 * @return 角色对象信息
	 */
	@Select("select r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status, r.del_flag, r.create_time, r.remark "
			+ "from t_role r " + "where r.del_flag = '0' and r.role_id = #{roleId}")
	public Role selectRoleById(Long roleId);

	/**
	 * 通过角色ID删除角色
	 * 
	 * @param roleId 角色ID
	 * @return 结果
	 */
	@Update("update t_role set del_flag = '2' where role_id = #{roleId}")
	public int deleteRoleById(Long roleId);

	/**
	 * 批量角色用户信息
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Update("<script>update t_role set del_flag = '2' where role_id in "
			+ "<foreach collection='array' item='roleId' open='(' separator=',' close=')'>" + "#{roleId}"
			+ "</foreach></script>")
	public int deleteRoleByIds(Long[] ids);

	/**
	 * 修改角色信息
	 * 
	 * @param role 角色信息
	 * @return 结果
	 */
	@Update("<script>update t_role " + "<set>"
			+ "<if test=\"roleName != null and roleName != ''\">role_name = #{roleName},</if>"
			+ "<if test=\"roleKey != null and roleKey != ''\">role_key = #{roleKey},</if>"
			+ "<if test=\"roleSort != null and roleSort != ''\">role_sort = #{roleSort},</if>"
			+ "<if test=\"dataScope != null and dataScope != ''\">data_scope = #{dataScope},</if>"
			+ "<if test=\"status != null and status != ''\">status = #{status},</if>"
			+ "<if test=\"remark != null\">remark = #{remark},</if>"
			+ "<if test=\"updateBy != null and updateBy != ''\">update_by = #{updateBy},</if>"
			+ "update_time = sysdate()" + "</set>" + "where role_id = #{roleId}</script>")
	public int updateRole(Role role);

	/**
	 * 新增角色信息
	 * 
	 * @param role 角色信息
	 * @return 结果
	 */
	@Insert("<script>insert into t_role(" + "<if test=\"roleId != null and roleId != 0\">role_id,</if>"
			+ "<if test=\"roleName != null and roleName != ''\">role_name,</if>"
			+ "<if test=\"roleKey != null and roleKey != ''\">role_key,</if>"
			+ "<if test=\"roleSort != null and roleSort != ''\">role_sort,</if>"
			+ "<if test=\"dataScope != null and dataScope != ''\">data_scope,</if>"
			+ "<if test=\"status != null and status != ''\">status,</if>"
			+ "<if test=\"remark != null and remark != ''\">remark,</if>"
			+ "<if test=\"createBy != null and createBy != ''\">create_by,</if>" + "create_time" + ")values("
			+ "<if test=\"roleId != null and roleId != 0\">#{roleId},</if>"
			+ "<if test=\"roleName != null and roleName != ''\">#{roleName},</if>"
			+ "<if test=\"roleKey != null and roleKey != ''\">#{roleKey},</if>"
			+ "<if test=\"roleSort != null and roleSort != ''\">#{roleSort},</if>"
			+ "<if test=\"dataScope != null and dataScope != ''\">#{dataScope},</if>"
			+ "<if test=\"status != null and status != ''\">#{status},</if>"
			+ "<if test=\"remark != null and remark != ''\">#{remark},</if>"
			+ "<if test=\"createBy != null and createBy != ''\">#{createBy},</if>" + "sysdate()" + ")</script>")
	@Options(useGeneratedKeys = true, keyProperty = "roleId")
	public int insertRole(Role role);

	/**
	 * 校验角色名称是否唯一
	 * 
	 * @param roleName 角色名称
	 * @return 角色信息
	 */
	@Select("select r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status, r.del_flag, r.create_time, r.remark "
			+ "from t_role r " + "where r.role_name=#{roleName} and r.del_flag = '0' limit 1")
	public Role checkRoleNameUnique(String roleName);

	/**
	 * 校验角色权限是否唯一
	 * 
	 * @param roleKey 角色权限
	 * @return 角色信息
	 */
	@Select("select r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status, r.del_flag, r.create_time, r.remark "
			+ "from t_role r " + "where r.role_key=#{roleKey} and r.del_flag = '0' limit 1")
	public Role checkRoleKeyUnique(String roleKey);
}
